<?php
namespace Admin\Model;
use Think\Model;
class AdminModel extends Model 
{   //允许插入数据表的字段
	protected $insertFields = array('adminname','password','chkcode','company_id');
	//更新时允许接收的字段
	protected $updateFields = array('id','adminname','password','company_id');
	protected $_validate = array(
		array('adminname', 'require', '管理员名称不能为空！', 1, 'regex', 3),
		array('adminname', '1,30', '管理员名称的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('password', 'require', '密码不能为空！', 1, 'regex', 1),
		array('password', '1,32', '密码的值最长不能超过 32 个字符！', 1, 'length', 3),
	);
	//登录时的表单验证规则
	public $_login_validate =array(
		array('adminname','require','用户名不能为空',1),
		array('adminname','require','密码不能为空',1),
		array('chkcode','require','验证码不能为空',1),
		array('chkcode','chk_verify','验证码错误',1,'callback'),
	);

	/**
	 * 验证验证码是否正确
	 * @param $code
	 * @param string $id
	 * @return bool
	 */
	public function chk_verify($code,$id='')
	{
		$verify = new \Think\Verify();
		return $verify->check($code,$id);
	}


	/**
	 * 登录
	 */
	public function login()
	{
		//从模型中取出管理员数据
		$username = $this->adminname;
		$password = $this->password;
		$user = $this->where(array(
			'adminname' => $username
		))->find();
		if($user){
			if($user['password'] == md5($password)){
				//成功，保存信息到session
				session('id',$user['id']);
				session('adminname',$user['adminname']);
				session('admin',$user);
				return TRUE;
			}
			else{
				$this->error('用户名或密码错误');
				return FALSE;
			}
		}
		else{
			$this->error('用户名不存在');
			return FALSE;
		}

	}
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')
			->field('a.*,GROUP_CONCAT(c.role_name) role_name')
			->join('left join __ADMIN_ROLE__ b on a.id =b.admin_id
			        left join __ROLE__ c on c.id =b.role_id ')
			->where($where)
			->group('a.id')
			->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		$data['password'] = md5($data['password']);
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if($data['password']){
			$data['password'] = md5($data['password']);
		}
		else{
			unset($data['password']);
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if($option['where']['id'] ==1)
		{
			$this->error = '超级管理员不能被删除';
			return FALSE;
		}

	}
	// 添加后
	protected function _after_insert($data, $option)
	{
		//$roleID
		$roleId = I('post.role_id');
		$roleModel = M('admin_role');
		foreach ($roleId as $v){
			$roleModel->add(array(
				'role_id' => $v,
				'admin_id' => $data['id']
		));
		}
	}
	/************************************ 其他方法 ********************************************/
	/**
	 * 退出，删除session
	 */
	public function logout()
	{
       session(null);
	}
	/**
	 * 制作验证码
	 */
	public function chkcode(){
		$Verify = new \Think\Verify(array(
			'fontSize'  =>50,
			'length'  =>'4',
			'useNoise' =>'TRUE',
		));
		$Verify->entry();
	}

}